#include"SchoolBus.h"

void SchoolBus::UpdateStationTime() {      //更新每个站停留的时间
	StationTime[Origin].push_back(this->StartTime);
	for (int i = 1; i <= Station.size() - 1; i++) {
		double minDistance;
		vector<Dot*>path = Floyd(Station[i - 1], Station[i], minDistance);   //获取两站之间的最短路径
		Time ArriveTime;
		Time ProcessTime;
		int CrossroadSum;
		CalculateRootTime(path, ArriveTime, ProcessTime, BusVelocity, CrossroadSum);
		StationTime[(Station[i])].push_back(   (StationTime[(Station[i-1])])[0] + ProcessTime);   //计算第一次到站时间
	}
	Time AtripTime = (StationTime[Terminal])[0] - (StationTime[Origin])[0];     //校车跑一趟所用的时间（单程，非往返）
	//对于那个map映射的数组，偶数下标（0，2...）表示从起点到终点的时间，奇数下标表示从终点到起点的时间，导航的时候一定会用到
	for (int i = 0; i <= Station.size() - 1; i++) {
		StationTime[(Station[i])].push_back(StartTime + AtripTime + AtripTime - (StationTime[(Station[i])])[0] - StartTime);   //记录第一趟返程对应的时间
	}
	for (;;) {
		int i = 0;
		for (; i <= Station.size() - 1; i++) {
			StationTime[(Station[i])].push_back((StationTime[(Station[i])])[StationTime[(Station[i])].size()-2]+ AtripTime + AtripTime);
			if ((StationTime[(Station[i])])[StationTime[(Station[i])].size() - 2] + AtripTime + AtripTime > EndTime) {
				break;
			}
		}
		if ((StationTime[(Station[i])])[StationTime[(Station[i])].size() - 2] + AtripTime + AtripTime > EndTime) {
			break;
		}
	}
	return;    //这样实际上只发了一趟车，但是可以通过取余操作来获取等待时间，因为只是间隔BusDuration而已
}

SchoolBus::SchoolBus(vector<Dot*>Sta) {     //构造函数
	this->Origin = Sta[0];
	this->Terminal = Sta[Sta.size() - 1];
	this->Station = Sta;
	Time* pStart = new Time(Starthour, Startmin, Startsec);
	Time* pEnd = new Time(Endhour, Endmin, Endsec);
	Time* pDuration = new Time(Durationhour, Durationmin, Durationsec);
	this->StartTime = *pStart;
	this->EndTime = *pEnd;
	this->BusDuration = *pDuration;
	this->UpdateStationTime();
}

SchoolBus CreateSchoolBusLine(vector<Dot*>Sta) {    //创建校车线路，以此传入所经过的所有站点
	SchoolBus* p = new SchoolBus(Sta);
	BusKu.push_back(*p);
	return *p;
}

void SchoolBus::ExtendStation(Dot* a) {   //扩展站点
	this->Station.push_back(a);
	this->Terminal = a;
	this->UpdateStationTime();
	return;
}

void SchoolBus::InsertStation(Dot* a, Dot* InsertPlace) {   //插入站点，在第二个参数之前插入
	for (vector<Dot*>::iterator it = this->Station.begin(); it != this->Station.end(); it++) {
		if ((*it)->GetID() == InsertPlace->GetID()) {
			this->Station.insert(it, a);
			this->UpdateStationTime();
			return;
		}
	}
	throw"Error:Can't find InsertPlace";
}

void SchoolBus::DeleteStation(Dot* a) {   //删除站点
	for (vector<Dot*>::iterator it = this->Station.begin(); it != this->Station.end(); it++) {
		if ((*it)->GetID() == a->GetID()) {
			if (it == this->Station.begin()) {    //删除的是起点
				this->Origin = (this->Station)[1];
			}
			else if ((*it)->GetID() == (this->Station)[this->Station.size() - 1]->GetID()) {   //删除的是终点
				this->Terminal== (this->Station)[(this->Station).size() - 2];
			}
			this->Station.erase(it);
			this->UpdateStationTime();
			return;
		}
	}
	throw"Error:Can't find InsertPlace";
}


int SchoolBus::GetStationIndex(Dot *target)  {//获取站点在数组中的下标
	int i = 0;
	for (; i <= Station.size(); i++) {
		if (Station[i] == target) {
			return i;
		}
	}
	throw"Error:Can't find wanted Station";
}